package br.com.fogas.medicao.activity.dialog;

import br.com.fogas.medicao.R;
import br.com.fogas.medicao.application.Constants;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class DialogObservacao extends DialogFragment {

	private String observacao;
	private DialogObservacaoListener listener;
	private Button btnConfirmar, btnCancelar;
	private TextView textObservacao;
	
	public interface DialogObservacaoListener {
		public void onConfirmarClick(String observacao);
	}

	public static DialogObservacao newInstance(DialogObservacaoListener listener, String observacao) {
		return new DialogObservacao()
		.setObservacao(observacao)
		.setListener(listener);
	}
	
	public DialogObservacao setObservacao(String observacao) {
		this.observacao = observacao;
		return this;
	}
	
	public DialogObservacao setListener(DialogObservacaoListener listener) {
		this.listener = listener;
		return this;
	}
	
	public DialogObservacao showDialog(FragmentManager fragmentManager) {
		show(fragmentManager, Constants.DIALOG_ALERT);
		return this;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View layout = inflater.inflate(R.layout.observacao_ocorrencia, null); 
		
		this.textObservacao = (TextView) layout.findViewById(R.id.etObservacao);
		this.textObservacao.setText(observacao);
		
		this.btnConfirmar = (Button) layout.findViewById(R.id.btnConfirmar);
		this.btnConfirmar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				DialogObservacao.this.dismiss();
				listener.onConfirmarClick(textObservacao.getText().toString());
			}
		});
		
		this.btnCancelar = (Button) layout.findViewById(R.id.btnCancelar);
		this.btnCancelar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				DialogObservacao.this.dismiss();
			}
		}); 
		
		Dialog dialog = new AlertDialog.Builder(getActivity())
		        .setView(layout)
				.setTitle(R.string.ocorrencia)
				.create();
		 
		return dialog;
	}
}